﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class TeacherBLL : CommonBLL<TeacherInfo, TeacherDAL>
    {
        public static IQueryable<TeacherInfo> GetList(string account, string name, int type = -1)
        {
            var re = GetList();
            re = re.Where(o => o.Type != 0&&o.Type!=4);
            if (type != -1)
                re = re.Where(o => o.Type == type);
            if (!string.IsNullOrEmpty(account))
                re = re.Where(o => o.Account == account);
            if (!string.IsNullOrEmpty(name))
                re = re.Where(o => o.Name.Contains(name));
            return re;
        }

        public static TeacherInfo Get(int id)
        {
            using (var dal = new TeacherDAL(Settings.ConnectionName))
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static void UpdateOnlineTime(int id, int time)
        {
            var info = Get(id);
            if (info != null)
            {
                info.OnlineTime += time;
                Edit(info);
            }
        }

        public static bool ChangePass(int id, string oldPass, string newPass)
        {
            var info = Get(id);
            if (info.Pass != oldPass)
                return false;
            info.Pass = newPass;
            return Edit(info);
        }

        public static List<TeacherInfo> GetAllTeacher()
        {
            var re = GetAll();
            return re.Where(o => o.Type != 0 && o.Type != 4).OrderBy(j => j.Type).ToList();
        }

        public static bool Del(int id)
        {
            using (var dal = new TeacherDAL(Settings.ConnectionName))
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }


        public static bool IsExist(string account, int id = 0)
        {
            using (var dal = new TeacherDAL(Settings.ConnectionName))
            {
                return dal.SelectSingle(o => o.Id != id && o.Account == account) != null;
            }
        }

        /// <summary>
        /// type指定类型的用户是否已存在
        /// </summary>
        /// <param name="type"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsExist(int type, int id = 0)
        {
            using (var dal = new TeacherDAL(Settings.ConnectionName))
            {
                return dal.SelectSingle(o => o.Id != id && o.Type == type) != null;
            }
        }

        public static TeacherInfo Login(string uid, string pass)
        {
            using (var dal = new TeacherDAL(Settings.ConnectionName))
            {
                return dal.SelectSingle(o => o.Account == uid && o.Pass == pass);
            }
        }


    }
}
